package com.mes.web.controller.biz;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import com.mes.common.annotation.Log;
import com.mes.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.mes.common.core.domain.TreeNode;
import com.mes.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import javax.validation.constraints.*;

import com.mes.system.domain.SysDictData;
import org.springframework.web.bind.annotation.*;
import com.mes.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.mes.biz.domain.BizApprovalSetting;
import com.mes.biz.service.BizApprovalSettingService;

/**
 * 审批权限设置Controller
 * @author hwg
 * @email hwg
 * @date 2023-05-01
 */
@RestController
@RequestMapping("/biz/bizApprovalSetting")
public class BizApprovalSettingController extends BaseController {

    @Autowired
    private BizApprovalSettingService bizApprovalSettingService;

    /**
     * 查询审批权限设置列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizApprovalSetting:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizApprovalSetting bizApprovalSetting, HttpServletRequest request, HttpServletResponse response) {
        bizApprovalSetting.setPage(new PageDomain(request, response));
        return R.data(bizApprovalSettingService.findPage(bizApprovalSetting));
    }

    /**
     * 获取审批权限设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizApprovalSetting:query')")
    @GetMapping(value = "/{id}")
    public R<BizApprovalSetting> detail(@PathVariable("id") String id) {
        return R.data(bizApprovalSettingService.get(id));
    }

    /**
     * 新增审批权限设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizApprovalSetting:add')")
    @Log(title = "审批权限设置", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizApprovalSetting bizApprovalSetting) {
        bizApprovalSetting.setTenantId(SecurityUtils.getTenantId());
        return R.status(bizApprovalSettingService.save(bizApprovalSetting));
    }

    /**
     * 修改审批权限设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizApprovalSetting:edit')")
    @Log(title = "审批权限设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizApprovalSetting bizApprovalSetting) {
        return R.status(bizApprovalSettingService.save(bizApprovalSetting));
    }

    /**
     * 删除审批权限设置
     */
    @PreAuthorize("@ss.hasPermi('biz:bizApprovalSetting:remove')")
    @Log(title = "审批权限设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizApprovalSettingService.deleteBizApprovalSettingByIds(ids));
    }


    /**
     * 导出审批权限设置列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizApprovalSetting:export')")
    @Log(title = "审批权限设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizApprovalSetting bizApprovalSetting) {
        List<BizApprovalSetting> list = bizApprovalSettingService.findList(bizApprovalSetting);
        ExcelUtil<BizApprovalSetting> util = new ExcelUtil<BizApprovalSetting>(BizApprovalSetting.class);
        return util.exportExcel(list, "审批权限设置数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    @GetMapping(value="/searchDocumentTypeList")
    public R searchDocumentTypeList(String documentTypeName) {
        List<SysDictData> documentTypeList = DictUtils.getDictList("document_type");
        documentTypeList = documentTypeList.stream().filter(dict->dict.getDictLabel().contains(documentTypeName)).collect(Collectors.toList());
       return R.data(documentTypeList);
    }

}
